import os

# from moviepy.editor import VideoFileClip

'''
检查视频尺寸并删除指定尺寸范围一下的视频
'''
def get_filesize(filename):
    u"""
    获取文件大小（M: 兆）
    """
    file_byte = os.path.getsize(filename)
    # return self.sizeConvert(file_byte)
    return file_byte


# def get_file_times(self, filename):
#     u"""
#     获取视频时长（s:秒）
#     """
#     clip = VideoFileClip(filename)
#     file_time = self.timeConvert(clip.duration)
#     return file_time


def sizeConvert(size):  # 单位换算
    K, M, G = 1024, 1024 ** 2, 1024 ** 3
    if size >= G:
        return str(size / G) + 'G Bytes'
    elif size >= M:
        return str(size / M) + 'M Bytes'
    elif size >= K:
        return str(size / K) + 'K Bytes'
    else:
        return str(size) + 'Bytes'


def timeConvert(size):  # 单位换算
    M, H = 60, 60 ** 2
    if size < M:
        return str(size) + u'秒'
    if size < H:
        return u'%s分钟%s秒' % (int(size / M), int(size % M))
    else:
        hour = int(size / H)
        mine = int(size % H / M)
        second = int(size % H % M)
        tim_srt = u'%s小时%s分钟%s秒' % (hour, mine, second)
        return tim_srt


def checkanddeletevideo(file_path):
    count = 0
    try:
        for dirpath, dirnames, filenames in os.walk(file_path):
            for filename in filenames:
                filename = dirpath.replace("\\", "/") + '/' + filename
                if filename.find(".mp4") > 0 and get_filesize(filename) < 10 * 1024 ** 2:
                    if not filename.find("徐雅") > 0:
                        print('=========================================================================')
                        print(filename)
                        os.remove(filename)
                        count += 1
                        print("remove success: " + filename)
        print(count)
    except:
        print("except")


if __name__ == "__main__":
    checkanddeletevideo('D:/videobypy')
    # checkanddeletevideo('D:\HYLiteResources/video')
